package cn.mxz.robots.web.service;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import cn.mxz.robots.basic.remote.ExamControl;
import cn.mxz.robots.basic.remote.GkForward;
import cn.mxz.robots.pojo.Student;
import cn.mxz.robots.util.DeprecatedRedisGK;
import cn.mxz.robots.util.RobotsUtlis;

/**
 * 高考service
 * 
 * @author cuizhennan<cuizhennan@mouge.wang>
 * @since 2015-06-19
 */
@Scope("prototype")
@Component
public class GkService
{
	@Autowired
	private ExamControl examControl;

	public String forwardService(String provinceId, String method, Student student, String captcha)
	{
		return GkForward.forwardController(examControl, provinceId, method, student, captcha);
	}

	/**
	 * 成绩查询
	 */
	public String queryService(String method, String token)
	{
		/* 登录Studnet的Key */
		String stuloginKey = "gk_login_info_" + token;
		String dataStu = DeprecatedRedisGK.get(stuloginKey);

		if (dataStu == null || "".equals(dataStu))
		{
			return "error_token";// token无效，重新绑定
		}

		if (RobotsUtlis.isElemetns(dataStu, "\\{.*\\}"))
		{
			JSONObject json = JSONObject.fromObject(dataStu);
			String province_id = json.getString("province_id");
			String student_id = json.getString("student_id");
			String student_pwd = json.getString("student_pwd");
			String id_card = json.getString("id_card");
			String account = json.getString("account");
			String type = json.getString("type");

			// 获得个人信息，进行成绩查询
			return forwardService(province_id, method, new Student(student_id, student_pwd, id_card, account, type), null);
		}

		return "Method [queryService] JSON Exception.data ==>" + dataStu;
	}
}
